package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.AgeUtil;
import com.whw.fy.utils.GetEveryDate;
import com.whw.fy.utils.WordUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.whw.fy.service.TeachSummaryService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;import java.util.List;

@Service
public class TeachSummaryServiceImpl implements TeachSummaryService {

    @Resource
    private TeachSummaryMapper teachSummaryMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private TechnicalTypeMapper technicalTypeMapper;
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Message deleteByPrimaryKey(Integer teachId) {
        return teachSummaryMapper.deleteByPrimaryKey(teachId) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message insertSelective(TeachSummary record) {
        return teachSummaryMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer teachId) {
        return Message.success(null).add(teachSummaryMapper.selectByPrimaryKey(teachId));
    }

    @Override
    public Message updateByPrimaryKeySelective(TeachSummary record) {
        return teachSummaryMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message selectAll() {
        List<TeachSummary> teachSummaryList = teachSummaryMapper.selectAll();
        return Message.success(null).add(initTeachSummaryList(teachSummaryList));
    }


    @Override
    public Message selectByStudentId(int studentId) {
        return Message.success(null).add(teachSummaryMapper.selectByTeachStuid(studentId));
    }

    @Override
    public Message downloadByPrimaryKey(String StudentId) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String url = WordUtil.getWordUrl(Recordmap,simpleDateFormat.format(record.getRecordListentime())+record.getStudentName()+"听课记录.doc","模板10.xml",globalConfig.hostUrl,globalConfig.globalFilePath);
        return Message.success(null).add("暂停");
    }

    @Override
    public int batchInsert(List<TeachSummary> list) {
        return teachSummaryMapper.batchInsert(list);
    }

    @Override
    public Message updateStatus(int status, int teachId) {
        return teachSummaryMapper.updateTeachStatusByTeachId(status, teachId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateReplyTeachingDirector(TeachSummary teachSummary) {
        return teachSummaryMapper.updateByPrimaryKeySelective(teachSummary) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateReplyPrincipalTeacher(TeachSummary teachSummary) {
        return teachSummaryMapper.updateByPrimaryKeySelective(teachSummary) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectNewOneByStudentId(int studentId) {
        return Message.success(null).add(teachSummaryMapper.selectTopOneByTeachStuid(studentId));
    }

    @Override
    public int insert(TeachSummary record) {
        return teachSummaryMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(TeachSummary record) {
        return teachSummaryMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<TeachSummary> list) {
        return teachSummaryMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<TeachSummary> list) {
        return teachSummaryMapper.updateBatchSelective(list);
    }

    @Override
    public Message selectallByMedicalDirectorIdJurisdictionZero(int staffId) {
        List<Student> studentList = studentMapper.selectByStudentTreatMedicalDirectorId(staffId);
        List<TeachSummary> teachSummaryArrayList = new ArrayList<>();
        for (Student student:studentList){
            List<TeachSummary> teachSummaryList = teachSummaryMapper.selectByTeachStuid(student.getStudentId());
            teachSummaryArrayList.addAll(teachSummaryList);
        }
        return Message.success(null).add(initTeachSummaryList(teachSummaryArrayList));
    }

    @Override
    public Message selectallByTeacherIdJurisdictionEight(int staffId) {
        return Message.success(null).add(initTeachSummaryList(teachSummaryMapper.selectByTeachPostUserId(staffId)));
    }

    @Override
    public Message selectallByTeachingDirectorJurisdictionSix(int staffId) {
        List<Student> studentList = studentMapper.selectByStudentTreatTeachingDirectorId(staffId);
        List<TeachSummary> teachSummaryArrayList = new ArrayList<>();
        for (Student student:studentList){
            List<TeachSummary> teachSummaryList = teachSummaryMapper.selectByTeachStuid(student.getStudentId());
            teachSummaryArrayList.addAll(teachSummaryList);
        }
        return Message.success(null).add(initTeachSummaryList(teachSummaryArrayList));
    }

    @Override
    public Message selectallByprincipalTeacherJurisdictionSeven(int staffId) {
        List<Student> studentList = studentMapper.selectAllByStudentTreatPrincipalTeacherId(staffId);
        List<TeachSummary> teachSummaryArrayList = new ArrayList<>();
        for (Student student:studentList){
            List<TeachSummary> teachSummaryList = teachSummaryMapper.selectByTeachStuid(student.getStudentId());
            teachSummaryArrayList.addAll(teachSummaryList);
        }
        return Message.success(null).add(initTeachSummaryList(teachSummaryArrayList));
    }

    private List<TeachSummary> initTeachSummaryList(List<TeachSummary> teachSummaryList){
        Date nowdate = new Date();
        for (TeachSummary teachSummary : teachSummaryList) {

            Student student = studentMapper.selectByPrimaryKey(teachSummary.getTeachStuid());
            teachSummary.setStudentName(student.getStudentName());
            //根据学生出生日期动态设置学生年龄
            if (student.getStudentBirth() != null) {
                teachSummary.setStudentAge(AgeUtil.getAgeByBirth(student.getStudentBirth()));
            } else {
                teachSummary.setStudentAge(-1);
            }
            switch (student.getStudentSex()) {
                case "0":
                    teachSummary.setStudentSex("男");
                    break;
                case "1":
                    teachSummary.setStudentSex("女");
                    break;
            }

            //根据学生加入中心日期动态设置学生加入时长
            teachSummary.setStudentJoinTime(GetEveryDate.GetTimeBetween(student.getStudentJoinCenter(), nowdate));
            Clazz clazz = clazzMapper.selectByPrimaryKey(teachSummary.getTeachClazzId());
            teachSummary.setClazzType(clazz.getClazz_type().getClazzTypeName());
            Lesson lesson = lessonMapper.selectByPrimaryKey(clazz.getClassCourseId());
            teachSummary.setTechnicalTypeName(lesson.getTechnicalType().getTechnicalTypeName());
            teachSummary.setTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassTeacherId()).getStaffName());
            teachSummary.setResponsibleTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassResponsibleTeacherId()).getStaffName());
        }
        return teachSummaryList;
    }
}





